package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/cargo/contractProduct")
@Controller
public class ContractProductController extends BaseController{
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @RequestMapping(value = "/list",name = "进入到货物界面")
    public String findAll(String contractId, @RequestParam(value = "page",defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int page){
        ContractProductExample example = new ContractProductExample();
        example.createCriteria().andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(example, pageNum, page);
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",list);
        request.setAttribute("page",pageInfo);
        request.setAttribute("contractId",contractId);
        return "cargo/product/product-list";
    }
    @RequestMapping(value = "/edit",name = "保存货物以及更新货物")
    public String edit(ContractProduct contractProduct, MultipartFile productPhoto) throws Exception {
        System.out.println(productPhoto.getOriginalFilename());
        //文件上传
        String upload=null;
        try {
            if(productPhoto!=null) {
                upload=fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            upload=null;
        }
        contractProduct.setProductImage(upload);
        if(StringUtils.isBlank(contractProduct.getId())) {
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCompanyId(getCompanyId());
            contractProduct.setCompanyName(getCompanyName());
            contractProductService.save(contractProduct);
        }else {
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractProduct.getContractId();
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到修改界面")
    public String toupdate(String id){
        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct",contractProduct);
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",list);
        return "/cargo/product/product-add";
    }
    @RequestMapping(value = "/delete",name = "删除货物")
    public String delete(String id,String contractId){
        contractProductService.delete(id);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }
    @RequestMapping(value = "toImport",name = "跳转到上传货物界面")
    public String toImport(String contractId){
        request.setAttribute("contractId",contractId);
        return "cargo/product/product-import";
    }
    @RequestMapping(value = "import",name = "上传货物")
    public String importExcel(String contractId,MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(is);
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        int lastRowNum = sheetAt.getLastRowNum();
        XSSFRow row=null;
        List<ContractProduct> list=new ArrayList<>();
        for (int i = 1; i <= lastRowNum; i++) {
            row= sheetAt.getRow(i);
            XSSFCell cell = row.getCell(1);
            ContractProduct contractProduct = new ContractProduct();//货物
            String factoryName = cell.getStringCellValue();//生产厂家
            contractProduct.setFactoryName(factoryName);
            String productNo = row.getCell(2).getStringCellValue();//货号
            contractProduct.setProductNo(productNo);
            Integer cnumber = ((Double)row.getCell(3).getNumericCellValue()).intValue();//数量
            contractProduct.setCnumber(cnumber);
            String packingUnit = row.getCell(4).getStringCellValue();//包装单位(PCS/SETS)
            contractProduct.setPackingUnit(packingUnit);
            String loadingRate = row.getCell(5).getNumericCellValue() + ""; //装率
            contractProduct.setLoadingRate(loadingRate);
            Integer boxNum = ((Double) row.getCell(6).getNumericCellValue()).intValue();//数量
            contractProduct.setBoxNum(boxNum);
            Double price = row.getCell(7).getNumericCellValue(); //单价
            contractProduct.setPrice(price);
            String productDesc = row.getCell(8).getStringCellValue(); // 货物描述
            contractProduct.setProductDesc(productDesc);
            String productRequest = row.getCell(9).getStringCellValue(); // 要求
            contractProduct.setProductRequest(productRequest);
            contractProduct.setContractId(contractId);
            contractProduct.setCompanyId(getCompanyId());
            contractProduct.setCompanyName(getCompanyName());
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCreateBy(getUser().getId());
            contractProduct.setCreateDept(getUser().getDeptId());
            contractProduct.setCreateTime(new Date());
            list.add(contractProduct);
        }
        contractProductService.saveList(list);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }
}
